<template>
  <div class="login-container">
    <div class="login-box">
      <h2 class="login-title">用户登录</h2>
      <form @submit.prevent="handleSubmit" class="login-form">
        <div class="form-group">
          <label for="username">用户名</label>
          <input
            v-model="form.username"
            type="text"
            id="username"
            placeholder="请输入用户名"
            :class="{ 'is-invalid': errors.username }"
          />
          <div v-if="errors.username" class="error-message">
            {{ errors.username }}
          </div>
        </div>

        <div class="form-group">
          <label for="password">密码</label>
          <input
            v-model="form.password"
            type="password"
            id="password"
            placeholder="请输入密码"
            :class="{ 'is-invalid': errors.password }"
          />
          <div v-if="errors.password" class="error-message">
            {{ errors.password }}
          </div>
        </div>

        <div class="form-group remember-me">
          <input v-model="form.remember" type="checkbox" id="remember" />
          <label for="remember">记住我</label>
        </div>

        <button type="submit" class="login-btn" :disabled="loading">
          <span v-if="!loading">登录</span>
          <span v-else>登录中...</span>
        </button>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useRouter } from "vue-router";
import { useUserStore } from "@/stores/user";

const router = useRouter();
const userStore = useUserStore();

const form = ref({
  username: "",
  password: "",
  remember: false,
});

const errors = ref({
  username: "",
  password: "",
});

const loading = ref(false);

const validateForm = () => {
  let isValid = true;
  errors.value = { username: "", password: "" };

  if (!form.value.username.trim()) {
    errors.value.username = "请输入用户名";
    isValid = false;
  }

  if (!form.value.password) {
    errors.value.password = "请输入密码";
    isValid = false;
  } else if (form.value.password.length < 6) {
    errors.value.password = "密码长度不能少于6位";
    isValid = false;
  }

  return isValid;
};

const handleSubmit = async () => {
  if (!validateForm()) return;

  loading.value = true;

  try {
    await userStore.login({
      username: form.value.username,
      password: form.value.password,
    });

    // 登录成功后跳转
    const redirectPath = router.currentRoute.value.query.redirect || "/";
    router.push(redirectPath);

    // router.push("/");
  } catch (error) {
    alert(error.message || "登录失败，请重试");
  } finally {
    loading.value = false;
  }
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f5f5;
  background-image: linear-gradient(120deg, #a1c4fd 0%, #c2e9fb 100%);
}

.login-box {
  width: 100%;
  max-width: 400px;
  padding: 2rem;
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.login-title {
  text-align: center;
  margin-bottom: 1.5rem;
  color: #333;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.form-group label {
  font-weight: 500;
  color: #555;
}

.form-group input {
  padding: 0.75rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 1rem;
  transition: border-color 0.3s;
}

.form-group input:focus {
  outline: none;
  border-color: #4d90fe;
}

.form-group input.is-invalid {
  border-color: #ff4d4f;
}

.error-message {
  color: #ff4d4f;
  font-size: 0.875rem;
}

.remember-me {
  flex-direction: row;
  align-items: center;
  gap: 0.5rem;
}

.login-btn {
  padding: 0.75rem;
  background-color: #4d90fe;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.3s;
}

.login-btn:hover {
  background-color: #357ae8;
}

.login-btn:disabled {
  background-color: #cccccc;
  cursor: not-allowed;
}

@media (max-width: 480px) {
  .login-box {
    padding: 1.5rem;
    margin: 0 1rem;
  }
}
</style>
